package com.hyyt.shopjsp.home.controller;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hyyt.shopjsp.basic.pojo.BasicUsers;
import com.hyyt.shopjsp.basic.service.IBasicActorPurviewService;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.home.pojo.ShopHomeAdvertisement;
import com.hyyt.shopjsp.home.service.IShopHomeAdvertisementService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;

/**
 * Copyright (c) 2016 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/03/24 0019下午 5:11.
 */
@Controller
@RequestMapping("/back/homeAdvertisement")
public class ShopHomeAdvertisementController extends BaseController {
    private Logger logger = Logger.getLogger(ShopHomeAdvertisementController.class);
    private String basePath = "/back/shop/homeAdvertisement/";
    @Resource
    private IShopHomeAdvertisementService shopHomeAdvertisementService;

    /**
	 * 进入首页广告位管理
	 *
	 * @return 进入指定页面
	 */
	@RequestMapping("gotoHomeAdvertisementPage")
	public String gotoHomeAdvertisementPage(){
		return basePath+"listHomeAdvertisement";
	}

	/**
	 * 查询所有首页广告位
	 * @param title 主题
	 * @param isShow 是否显示
	 * @param showLocation 显示位置
	 * @param page 分页
	 * @param response 结果集
	 */
	@RequestMapping(value="listHomeAdvertisement",method=RequestMethod.POST)
	@ResponseBody
	public void listHomeAdvertisement(String title,String isShow,String showLocation,Page page, HttpServletResponse response){
		Map<String, Object> param = new HashMap<>();
		if (Utils.objectIsNotEmpty(title)) {//是否检索名称
			param.put("title", title.trim());
		}
		if (Utils.objectIsNotEmpty(isShow)) {//是否检索类型名称
			param.put("isShow", isShow);
		}
		if (Utils.objectIsNotEmpty(showLocation)) {//是否检索类型名称
			param.put("showLocation", showLocation);
		}
		param.put("sortColumns", "advertisementId desc");
		int totalRecordCount =shopHomeAdvertisementService.countShopHomeAdvertisement(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<ShopHomeAdvertisement> list =shopHomeAdvertisementService.queryShopHomeAdvertisement(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", list);// rows键 存放每页记录 list
		ResultMsg.create(param, response);  //把内容发送到页面
	}

	/**
     * 根据首页广告位id获得对象
     *
     * @param advertisementId
     * @param response
     */
    @RequestMapping(value = "getHomeAdvertisementObject", method = RequestMethod.POST)
    @ResponseBody
	public ResultMsg getHomeAdvertisementObject(Integer advertisementId, HttpServletResponse response) {
		if (Utils.objectIsNotEmpty(advertisementId)) {
			ShopHomeAdvertisement shopHomeAdvertisement = shopHomeAdvertisementService.loadShopHomeAdvertisement(advertisementId);
			return ResultMsg.create(true, null, FastJsonUtils.toJSONString(shopHomeAdvertisement));
		}else {
			return ResultMsg.create(false);
		}
	}

    /**
     * 保存或者修改首页广告位
     *
     * @param shopHomeAdvertisement
     * @return
     */
    @RequestMapping(value = "saveOrUpdateHomeAdvertisement", method = RequestMethod.POST)
    @ResponseBody
	public ResultMsg saveOrUpdateHomeAdvertisement(ShopHomeAdvertisement shopHomeAdvertisement) {
		if (Utils.objectIsNotEmpty(shopHomeAdvertisement)) {
			BasicUsers basicUsers = getSessionUsers();
			if (Utils.objectIsEmpty(shopHomeAdvertisement.getAdvertisementId())) {
				Timestamp createTime = new Timestamp(System.currentTimeMillis()); 
				shopHomeAdvertisement.setCreateTime(createTime);
				shopHomeAdvertisement.setPublishUser(basicUsers.getUserName());
				shopHomeAdvertisementService.saveShopHomeAdvertisement(shopHomeAdvertisement);
			} else {
				Timestamp updateTime = new Timestamp(System.currentTimeMillis()); 
				shopHomeAdvertisement.setUpdateTime(updateTime);
				shopHomeAdvertisement.setModifyUser(basicUsers.getUserName());
				shopHomeAdvertisementService.updateShopHomeAdvertisement(shopHomeAdvertisement);
			}
			return ResultMsg.create(true);
		}else {
			return ResultMsg.create(false);
		}
	}

    /**
     * 根据传入的首页广告位id
     *
     * @param ids
     */
    @RequestMapping(value = "deleteHomeAdvertisement", method = RequestMethod.POST)
    @ResponseBody
	public ResultMsg deleteHomeAdvertisement(String ids) {
		if (Utils.objectIsNotEmpty(ids)) {
			shopHomeAdvertisementService.deleteShopHomeAdvertisement(Integer.parseInt(ids));
			return ResultMsg.create(true);
		}else {
			return ResultMsg.create(false);
		}
	}
}
